﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;

namespace FClient
{
    public class MyRequest
    {
        public static string HttpGet(string url)
        {
            try
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.Method = "GET";

                string result = null;
                using (HttpWebResponse resp = req.GetResponse()
                                              as HttpWebResponse)
                {
                    using (var reader = new StreamReader(resp.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                return result;
            }
            catch (Exception e)
            {
                return "";
            }
        }

        public static void Login(string username, string password)
        {
            string key = "dj0yJmk9ZmF6eUdUWGlGWWNOJmQ9WVdrOU5GQjBkRFYwTlRZbWNHbzlOakF4TVRZeE9EWXkmcz1jb25zdW1lcnNlY3JldCZ4PWM4";
            string url = "https://login.yahoo.com/WSLogin/V1/get_auth_token?&login=" + username + "&passwd=" + password + "&oauth_consumer_key=" + key;

            string token = HttpGet(url);
        }
        public static void createSession()
        {
            HttpWebRequest req = WebRequest.Create("/v1/session") as HttpWebRequest;
            req.Method = "POST";
            req.Host = "developer.messenger.yahooapis.com";
            req.ContentType = "application/json;charset=utf-8";

            string result = null;
            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                using (var reader = new StreamReader(resp.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
            }
        }

        public static bool Delete(string id)
        {
            HttpWebRequest req = WebRequest.Create("http://localhost:8000/api/student/" + id) as HttpWebRequest;
            req.Method = "DELETE";

            req.Accept = "text/json";
            bool result = false;
            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                using (var reader = new StreamReader(resp.GetResponseStream()))
                {

                    string obj = reader.ReadToEnd();
                    result = Convert.ToBoolean(obj);
                }

            }

            return result;
        }

        public static Image GetImageStu(string MSSV)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create("http://localhost:8000/api/student/" + MSSV + "/image") as HttpWebRequest;
                request.Method = "GET";

                using (HttpWebResponse resp = request.GetResponse()
                                              as HttpWebResponse)
                {
                    using (Stream str = resp.GetResponseStream())
                    {
                        return Image.FromStream(str);
                    }
                }
            }
            catch (Exception e)
            {
                return null;
            }

        }

        public static bool PutImageStu(string ID, string srcimg)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create("http://localhost:8000/api/student/" + ID + "/image")
                                     as HttpWebRequest;
                request.Method = "PUT";


                request.ContentType = "image/jpeg";
                FileStream f = new FileStream(srcimg, FileMode.Open, FileAccess.Read); // this file stream will be closed by lower layers of web api for you once the response is completed.
                using (Stream str = request.GetRequestStream())
                {
                    f.CopyTo(str);


                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    string res = sr.ReadToEnd();
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }

        }
    }
}